import { RouteRecordRaw } from 'vue-router'

export default function (menus: any[]): RouteRecordRaw[] {
  // 获取
  const routes: RouteRecordRaw[] = []
  // 获取文件目录解构
  // 先默认加载所有文件模块到数组
  const allRoutes: RouteRecordRaw[] = []
  const routesFiles = require.context('../router/main', true, /\.ts/)
  routesFiles.keys().forEach((file) => {
    const route = require('../router/main' + file.slice(1))
    allRoutes.push(route.default)
  })

  // 根据menu查找过滤
  const _recurseGetRoute = (menus: any[]) => {
    for (const menu of menus) {
      if (menu.type === 2) {
        const route = allRoutes.find((route) => route.path === menu.url)
        if (route) routes.push(route)
      } else {
        _recurseGetRoute(menu.children)
      }
    }
  }
  // 递归查找符合条件的路由
  _recurseGetRoute(menus)
  return routes
}
